package com.lkc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lkc.domain.ResponseResult;
import com.lkc.domain.entity.StudentTeacher;
import com.lkc.domain.entity.User;
import com.lkc.domain.vo.PageVo;
import com.lkc.domain.vo.StudentVo;
import com.lkc.domain.vo.UpdateVo;
import com.lkc.enums.AppHttpCodeEnum;
import com.lkc.exception.SystemException;
import com.lkc.mapper.StudentTeacherMapper;
import com.lkc.mapper.UserMapper;
import com.lkc.service.UserService;
import com.lkc.utils.BeanCopyUtils;
import com.lkc.utils.security.LoginUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * (User)表服务实现类
 *
 * @author 椰咩咩咩
 * @since 2022-12-23 13:42:48
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private StudentTeacherMapper studentTeacherMapper;

    @Override
    public ResponseResult students(Integer pageNum, Integer pageSize) {
        if (!LoginUser.isTeacher()) {
            throw new SystemException(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }

        User user = LoginUser.getUser();

        List<Long> studentIds = studentTeacherMapper.getSIdsByTid(user.getId());

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(User::getId, studentIds);
        Page<User> page = new Page<>(pageNum, pageSize);
        page(page, wrapper);
        List<StudentVo> studentVos = BeanCopyUtils.copyBeanList(page.getRecords(), StudentVo.class);
        return ResponseResult.okResult(new PageVo(studentVos, page.getTotal()));
    }

    @Override
    public ResponseResult getStudentById(Long id) {
        if (!LoginUser.isTeacher()) {
            throw new SystemException(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }
        User user = getById(id);
        StudentVo studentVo = BeanCopyUtils.copyBean(user, StudentVo.class);
        return ResponseResult.okResult(studentVo);
    }

    @Override
    public ResponseResult addStudent(User student) {
        if (!LoginUser.isTeacher()) {
            throw new SystemException(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }
        boolean save = save(student);
        StudentTeacher studentTeacher = new StudentTeacher(student.getId(), LoginUser.getLoginUserId());
        int insert = studentTeacherMapper.insert(studentTeacher);
        return save && insert > 0 ? ResponseResult.okResult(new UpdateVo(AppHttpCodeEnum.SAVE_SUCCESS)) : ResponseResult.errorResult(AppHttpCodeEnum.SAVE_ERROR);
    }

    @Override
    public ResponseResult updateStudent(User student) {
        if (!LoginUser.isTeacher()) {
            throw new SystemException(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }
        int i = userMapper.updateById(student);
        return i > 0 ? ResponseResult.okResult(new UpdateVo(AppHttpCodeEnum.MODIFY_SUCCESS)) : ResponseResult.errorResult(AppHttpCodeEnum.MODIFY_ERROR);
    }
}

